package com.example.hotelmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.hotelmanager.entity.*;
import com.example.hotelmanager.entity.vo.Response;
import com.example.hotelmanager.entity.vo.ResponseMsg;
import com.example.hotelmanager.service.*;
import com.example.hotelmanager.utils.JwtUtill;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/29 23:47
 */
@RestController
@Slf4j
@CrossOrigin
public class BookMsgController {
    @Autowired
    private CheckInService checkInService;
    @Autowired
    private JwtUtill jwtUtill;
    @Autowired
    private FrontService frontService;
    @Autowired
    private BookMsgService bookMsgService;
    @Autowired
    private GuestService guestService;
    @GetMapping("/getAllBookMsgs")
    public Response getAllBookMsgs (HttpServletRequest request){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            List<BookMsg> bookMsgs = bookMsgService.list();
            if(bookMsgs != null && bookMsgs.size()>0){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("bookMsgs",bookMsgs);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    @PutMapping("/updateBookMsg")
    public Response updateBookMsg(HttpServletRequest request,int id, Long fromTime,Long toTime,String rank){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            BookMsg bookMsg = bookMsgService.getById(id);
            bookMsg.setState(0);
            bookMsg.setResultRoom("");
            bookMsg.setFromTime(new Timestamp(fromTime));
            bookMsg.setToTime(new Timestamp(toTime));
            bookMsg.setGrade(rank);
            bookMsgService.saveOrUpdate(bookMsg);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    /**
     * 为预定信息分配安排房间
     * @param request
     * @param bookMsgId
     * @param roomId
     * @return
     */
    @PutMapping("/assignment")
    public Response assignment(HttpServletRequest request, int bookMsgId,String roomId){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            BookMsg bookMsg = bookMsgService.getById(bookMsgId);
            //获取当前的预定信息
            List<BookMsg> bookMsgs = bookMsgService.getBookMsgByTime(bookMsg.getFromTime(),bookMsg.getToTime());
            //获取当前正在入住的信息
            List<CheckIn> checkIns = checkInService.getValidCheckIns(bookMsg.getFromTime(),bookMsg.getToTime());
            int f = 0;
            for(BookMsg bookMsg1:bookMsgs){
                if(bookMsg1.getResultRoom() == roomId){//已预定
                    f = 1;
                    break;
                }
            }
            for(CheckIn checkIn:checkIns){//已入住
                if(f == 1)
                    break;
                if(checkIn.getRoomId() == roomId){
                    f = 1;
                    break;
                }
            }
            if(f == 0){
                bookMsg.setState(1);
                bookMsg.setResultRoom(roomId);
                bookMsgService.saveOrUpdate(bookMsg);
                String token = jwtUtill.updateJwt(num);
                return (new Response()).success(token);
            }
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    @PostMapping("/addBookMsg")
    public Response addBookMsg(HttpServletRequest request, Long fromTime,Long toTime,String contact,String idCard,String name,String rank){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            Guest guest = new Guest(idCard,name,contact);
            guestService.saveOrUpdate(guest);
            BookMsg bookMsg = new BookMsg(0,new Timestamp(fromTime),new Timestamp(toTime),idCard,rank,0,null);
            bookMsgService.saveOrUpdate(bookMsg);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    @DeleteMapping("/deleteBookMsg")
    public Response deleteBookMsg(HttpServletRequest request,int id){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            bookMsgService.removeById(id);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    @GetMapping("/getBookMsgByIdCard")
    public Response getBookMsgByIdCard(HttpServletRequest request,String idCard){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            LambdaQueryWrapper<BookMsg> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.like(BookMsg::getGuestIdCard,idCard);
            List<BookMsg> bookMsgs = bookMsgService.list(queryWrapper);
            if(bookMsgs != null && bookMsgs.size()>0){//形成新的token
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("bookMsgs",bookMsgs);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

}
